<?php
/*
 * Created on Dec 7, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

include_once("../configure/configure.php");		//--> global var
include_once("../lib/impl/KeywordImpl.php");
include_once("../admin/lib/utils/StringUtils.php");


set_time_limit(0);
header("Content-type:text/html; charset=utf-8");
   
deleteInvalidCoupon();




function deleteInvalidCoupon()
{
	$counter	= 0;
	
	$oKeywordImpl 	= new KeywordImpl(DB_TAG_COUPON);
	
	
	$sql = "SELECT id, name FROM coupon";
	
	echo "sql = $sql \n";
	
	$allData = $oKeywordImpl->getAllData(DB_TAG_COUPON, $sql);
	
	echo '<br>waitting update calculate count = ' . count($allData) . '<br>';
	foreach($allData as $arrRow){
		$couponId 	= $arrRow['id'];
		$name		= strtolower($arrRow['name']);

		$attributeId = $oKeywordImpl->getColumn(DB_TAG_COUPON, "SELECT attributeId FROM coupon_attributes WHERE couponId = $couponId AND attributeId != 34 AND attributeValue ='Promo Type'");

		if(empty($attributeId)){
			$pos1 = strrpos($name, '$');
			$pos2 = strrpos($name, '%');
			$pos3 = strripos($name, 'free shipping');
			$pos4 = strripos($name, 'free');
			
			if($pos1 > 0 || $pos2 > 0 || $pos3 > 0  || $pos4 > 0 || $pos1 === 0 || $pos2 === 0 || $pos3 === 0 || $pos4 === 0){
				
			}else{
				echo "***************************************************<br>";
				
				echo "<br> id = $couponId";
				echo "<br> name = $name";
				
					$sql	= "DELETE FROM coupon WHERE id = $couponId";
					$counter 	+= $oKeywordImpl->update(DB_TAG_COUPON, $sql);
				
					$sql	= "DELETE FROM coupon_attributes WHERE couponId = $couponId";
					$counter 	+= $oKeywordImpl->update(DB_TAG_COUPON, $sql);

					$sql	= "DELETE FROM coupon_images WHERE couponId = $couponId";
					$counter 	+= $oKeywordImpl->update(DB_TAG_COUPON, $sql);
					
					$sql	= "DELETE FROM coupon_category WHERE couponId = $couponId";
					$counter 	+= $oKeywordImpl->update(DB_TAG_COUPON, $sql);
				
			}
		}
		
	}
	
	echo "<br> counter = $counter";
}

?>
